import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import { getPlayingFilmList, getWillFilmList, getCinemaList ,getFilmInfo } from '@/http'
import createPersistedstate from 'vuex-persistedstate'

export default new Vuex.Store({
    state: {
        playingFilmList: [], //热映电影列表
        willFilmList:[], //即将上映
        cinemaList:[], // 影城列表
        getFilmInfo:[], //详情
    },
    mutations: {
        // 获取热映电影列表
        getPlaying(state,params={}) {
            //   数据请求
            getPlayingFilmList(params).then(res =>state.playingFilmList=res.data)
        },
        // 即将上映
        getWill(state,params={}){
            getWillFilmList(params).then(res=>state.willFilmList=res.data)
        },
        // 影城
        getCinema(state,params={}){
            getCinemaList(params).then(res => state.cinemaList = res.data)
        },
        // 详情
        getInfo(state, params = {}) {
            getFilmInfo(params).then(res =>{
                state.getFilmInfo = res.data.film
            })
        }
    },
    actions:{
        // 获取热映电影列表
        getPlayingAction(context,params={}){
             context.commit('getPlaying', params)
        },
        // 即将上映
        getWillAction(context,params={}){
             context.commit('getWill',params)
        },
        // 影城
        getCinemaAction(context, params = {}) {
             context.commit('getCinema', params)
        },
        // 详情
        getInfoAction(context, params = {}) {
             context.commit('getInfo', params)
        }
    },
    plugins: [createPersistedstate(
        {
            storage: window.sessionStorage,// 设置成临时性存储
            reducer: (val) => { // 存储指定的某一条数据
                console.log(111,val);
                return {
                    playingFilmList: val.playingFilmList, //热映
                    willFilmList: val.willFilmList, //即将上映
                    cinemaList: val.cinemaList, //影城
                    // getFilmInfo: val.getFilmInfo , // 详情
                     
                }
            }

        }
    )] //数据持久化插件配置成功

})